package com.wanban.network

import android.text.TextUtils
import com.wanban.adutils.AdUtils
import com.wanban.adutils.BuildConfig
import com.wanban.adutils.db.bean.AdContent
import com.wanban.adutils.db.bean.AdInfo
import com.wanban.adutils.tools.AdConstants
import com.wanban.tools.*


class SplashViewModel : BaseViewModel() {

    /**
     * login接口，检查app内容的
     */
    fun checkApp(callback: OnRetrofitResultListener<Boolean>) {
        doBaseHttp({
            // token超时时间
            val tokenOutTime = SPUtil.instance.tokenExpireTime
            if (System.currentTimeMillis() < tokenOutTime) { // 未超时
                val token = SPUtil.instance.token
                return@doBaseHttp if (TextUtils.isEmpty(token)) {
                    null
                } else {    // 保存sKey和iv
                    callback.onSuccess(SPUtil.instance.isAllowPosition)
                    token
                }
            }
            // 超时，直接请求新的
            return@doBaseHttp null
        }, {
            // 已经请求到token
            val appId = "100180001"
            val appKey = "18a4b60ce431ae2e4e9f33da2cf6cf4c"
            val timestamp = getSecondTimestamp()
            val sign = MD5Utils.getMD5String(
                "b277322e1ca4c4c5ef51242c900726c2",
                appId,
                appKey,
                timestamp.toString(),
                "notice"
            )
            val request = CheckAppRequest(appId, timestamp, sign)
            return@doBaseHttp RetrofitUtils.instance.getApi(Api::class.java).checkApp(request)
        }, object : OnRetrofitResultListener<CheckResponse> {
            override fun onSuccess(t: CheckResponse) {
                if (t.data == null || t.data?.length != 32) {
                    callback.onFailed("加载失败")
                    return
                }
                // 更新token超时时间
                val updateTime = System.currentTimeMillis() + t.duration * 1000
                SPUtil.instance.tokenExpireTime = updateTime
                // 保存token
                SPUtil.instance.token = t.data!!
                // 是否允许定位
                SPUtil.instance.isAllowPosition = t.ap
                callback.onSuccess(t.ap)
            }

            override fun onFailed(msg: String) {
                callback.onFailed(msg)
            }
        }, isCheckToken = false)
    }

    /**
     * 获取广告列表
     */
    fun getAdList(callback: OnRetrofitResultListener<List<AdContent>>) {
        doBaseHttp({
            val appId = getAppId()
            val appKey = getAppKey()
            val timestamp = getSecondTimestamp()
            val version = 1
            val sign = MD5Utils.getMD5String(version, appId, appKey, timestamp.toString(), getExtra())
            val request = AdRequest(
                version.toString(),
                appId,
                timestamp,
                sign
            )
            return@doBaseHttp RetrofitUtils.instance.getApi(Api::class.java).getAds(version.toString(), getCommonRequest(request))
        }, object : OnRetrofitResultListener<AdModel> {
            override fun onSuccess(t: AdModel) {
                val list = initAdList(t)
                if (list.isNullOrEmpty()) {
                    if (isNotActive()) return
                    callback.onFailed("请求失败")
                    return
                }
                if (isNotActive()) return
                // 初始化广告计划执行
                callback.onSuccess(list)
            }

            override fun onFailed(msg: String) {
                if (isNotActive()) return
                // 初始化广告计划执行
                callback.onFailed(msg)
            }
        })
    }


    private fun initAdList(adModel: AdModel): ArrayList<AdContent> {
        AdUtils.clearAdCache()
        // 测试广告
        changeTestAd(adModel)
        if (adModel.advertising.isNullOrEmpty()) {
            return ArrayList()
        }
        val list = ArrayList<AdContent>()
        adModel.advertising.forEach { entity ->
            if (!entity.advertises.isNullOrEmpty()) {
                val adContent = AdContent()
                adContent.adId = entity.ad_id
                adContent.adType = entity.template
                adContent.adRule = adModel.adInterval
                adContent.countDown = adModel.countDown
                // 广告时间间隔
                adContent.interval = entity.interval
                adContent.plan = adModel.plan
                adContent.adInfo = ArrayList()
                entity.advertises.forEach {
                    val adInfo = AdInfo()
                    adInfo.advertiser = it.advertisers
                    adInfo.ad_type = entity.template
                    adInfo.ad_position = it.advertising_id
                    adInfo.timeout = it.req_timeout
                    adInfo.isCache = it.cache_next
                    adInfo.ad_duration = it.auto_close
                    adInfo.adId = entity.ad_id
                    adInfo.interval = entity.interval
                    adContent.adInfo.add(adInfo)
                }
                list.add(adContent)
            }
        }
        AdUtils.insertAds(list)
        return list
    }

    private fun changeTestAd(it: AdModel) {
        if (BuildConfig.DEBUG) {
//            if (it.advertising.isNullOrEmpty()) {
//                it.advertising = ArrayList()
//            } else {
//                it.advertising?.clear()
//            }
            it.advertising?.clear()
//            it.interval = 30
//            it.advertising?.forEach {
//                if (it.ad_id == AdConstants.AD_ID_SPLASH) {
//                    it.advertises?.forEach {
//                        it.req_timeout = 60
//                    }
//                } else {
//                    it.advertises?.forEach {
//                        it.req_timeout = 30
//                    }
//                }
//
//                if (it.ad_id == AdConstants.AD_ID_SPLASH) {
//                    Log.e("wbAd", "---------测试，调整启动广告未ReopenAd---------")
//                    it.ad_id = AdConstants.AD_ID_REOPEN_AD
//                    it.template = AdConstants.ADTYPE_INTERSTITIAL
//                    it.advertises?.firstOrNull()?.apply {
//                        advertisers = AdConstants.ADVERTISER_IRON_SOURCE
//                        advertising_id = "5024152"
//                    }
//                }
//            }



            // 开屏Splash
            val splashAdList = ArrayList<AdModel.AdvertisingResponse.AdvertisesResponse>()
            val splashEntity = AdModel.AdvertisingResponse.AdvertisesResponse()
            splashEntity.advertisers = AdConstants.ADVERTISER_ADMOB
            splashEntity.advertising_id = "ca-app-pub-8138205203670003/7297538222"
//            splashEntity.advertisers = AdConstants.ADVERTISER_IRON_SOURCE
//            splashEntity.advertising_id = "5024156"
            splashEntity.cache_next = false
            splashEntity.req_timeout = 60
            splashEntity.auto_close = 6
            splashAdList.add(splashEntity)
            val splashAd = AdModel.AdvertisingResponse()
            splashAd.template = AdConstants.ADTYPE_SPLASH
//            splashAd.template = AdConstants.ADTYPE_INTERSTITIAL
            splashAd.advertises = splashAdList
            splashAd.interval = 0
            splashAd.ad_id = AdConstants.AD_ID_OPEN_AD_V2
            it.advertising.add(splashAd)

//
            // Reopen
            val reopenAdList = ArrayList<AdModel.AdvertisingResponse.AdvertisesResponse>()
            val reopenEntity = AdModel.AdvertisingResponse.AdvertisesResponse()
            reopenEntity.advertisers = AdConstants.ADVERTISER_ADMOB
            reopenEntity.advertising_id = "ca-app-pub-8138205203670003/7297538222"
//            reopenEntity.advertisers = AdConstants.ADVERTISER_IRON_SOURCE
//            reopenEntity.advertising_id = "10330689"
            reopenEntity.cache_next = false
            reopenEntity.req_timeout = 30
            reopenEntity.auto_close = 6
            reopenAdList.add(reopenEntity)
            val reopenAd = AdModel.AdvertisingResponse()
            reopenAd.template = AdConstants.ADTYPE_SPLASH
//            reopenAd.template = AdConstants.ADTYPE_INTERSTITIAL
            reopenAd.interval = 60
            reopenAd.advertises = reopenAdList
            reopenAd.ad_id = AdConstants.AD_ID_REOPEN_AD
            it.advertising.add(reopenAd)
////
//
//            // 详情页插屏
//            val interstitialAdList = ArrayList<AdModel.AdvertisingResponse.AdvertisesResponse>()
//            val adInfoEntity2 = AdModel.AdvertisingResponse.AdvertisesResponse()
//            adInfoEntity2.advertisers = AdConstants.ADVERTISER_IRON_SOURCE
//            adInfoEntity2.advertising_id = "5024156"
//            adInfoEntity2.cache_next = false
//            adInfoEntity2.req_timeout = 1
//            adInfoEntity2.auto_close = 3
//            interstitialAdList.add(adInfoEntity2)
//            val interstitialAdContent = AdModel.AdvertisingResponse()
//            interstitialAdContent.template = AdConstants.ADTYPE_INTERSTITIAL
//            interstitialAdContent.advertises = interstitialAdList
//            interstitialAdContent.interval = 60
//            interstitialAdContent.ad_id = AdConstants.AD_ID_VIDEO_DETAIL_INTERSTITIAL
//            it.advertising.add(interstitialAdContent)

//            // 解锁激励
//            val rewardedAdList = ArrayList<AdModel.AdvertisingResponse.AdvertisesResponse>()
//            val adInfoEntity3 = AdModel.AdvertisingResponse.AdvertisesResponse()
//            adInfoEntity3.advertisers = AdConstants.ADVERTISER_IRON_SOURCE
//            adInfoEntity3.advertising_id = "5024152"
//            adInfoEntity3.cache_next = false
//            adInfoEntity3.req_timeout = 60
//            adInfoEntity3.auto_close = 6
//            rewardedAdList.add(adInfoEntity3)
//            val unlockRewardedAd = AdModel.AdvertisingResponse()
//            unlockRewardedAd.template = AdConstants.ADTYPE_REWARDED
//            unlockRewardedAd.advertises = rewardedAdList
//            unlockRewardedAd.ad_id = AdConstants.AD_ID_LOCK_REWARD_AD
//            it.advertising.add(unlockRewardedAd)
//
//
//            // 详情banner
//            val bannerEntity = AdModel.AdvertisingResponse.AdvertisesResponse()
//            bannerEntity.advertisers = AdConstants.ADVERTISER_IRON_SOURCE
//            bannerEntity.advertising_id = "10330691"
//            bannerEntity.cache_next = false
//            bannerEntity.req_timeout = 60
//            bannerEntity.auto_close = 6
//
//            val bannerAdList = ArrayList<AdModel.AdvertisingResponse.AdvertisesResponse>()
//            bannerAdList.add(bannerEntity)
//
//            val bannerAd = AdModel.AdvertisingResponse()
//            bannerAd.template = AdConstants.ADTYPE_BANNER
//            bannerAd.advertises = bannerAdList
//            bannerAd.ad_id = AdConstants.AD_ID_VIDEO_DETAIL_BANNER
//
//            it.advertising.add(bannerAd)
//
//
//            // 暂停native
//            val nativeEntity = AdModel.AdvertisingResponse.AdvertisesResponse()
//            nativeEntity.advertisers = AdConstants.ADVERTISER_IRON_SOURCE
//            nativeEntity.advertising_id = "10330691"
//            nativeEntity.cache_next = false
//            nativeEntity.req_timeout = 60
//            nativeEntity.auto_close = 6
//
//            val nativeAdList = ArrayList<AdModel.AdvertisingResponse.AdvertisesResponse>()
//            nativeAdList.add(nativeEntity)
//
//            val nativeAd = AdModel.AdvertisingResponse()
//            nativeAd.template = AdConstants.ADTYPE_PAUSE_VIDEO
//            nativeAd.advertises = nativeAdList
//            nativeAd.ad_id = AdConstants.AD_ID_VIDEO_DETAIL_PAUSE
//
//            it.advertising.add(nativeAd)
        }
    }

    private fun getTestAdList(): ArrayList<AdModel.AdvertisingResponse> {
        val adList = ArrayList<AdModel.AdvertisingResponse>()

//            // native - 开屏
//            val openAdList = ArrayList<AdModel.AdvertisingResponse.AdvertisesResponse>()
//            val adInfoEntity1 = AdModel.AdvertisingResponse.AdvertisesResponse()
//            adInfoEntity1.advertisers = AdConstants.ADVERTISER_IRON_SOURCE
//            adInfoEntity1.advertising_id = "5024158"
//            adInfoEntity1.cache_next = true
//            adInfoEntity1.req_timeout = 5
//            adInfoEntity1.auto_close = 6
//            openAdList.add(adInfoEntity1)
//            val openAdContent = AdModel.AdvertisingResponse()
//            openAdContent.template = AdConstants.ADTYPE_OPEN
//            openAdContent.advertises = openAdList
//            openAdContent.ad_id = AdConstants.AD_ID_OPEN
//            it.advertising?.add(openAdContent)
//
//        // native - 暂停
//        val pauseAdList = ArrayList<AdModel.AdvertisingResponse.AdvertisesResponse>()
//        val adInfoPause = AdModel.AdvertisingResponse.AdvertisesResponse()
//        adInfoPause.advertisers = AdConstants.ADVERTISER_IRON_SOURCE
//        adInfoPause.advertising_id = "5024158"
//        adInfoPause.cache_next = true
//        adInfoPause.req_timeout = 10
//        adInfoPause.auto_close = 6
//        pauseAdList.add(adInfoPause)
//        val pauseAdContent = AdModel.AdvertisingResponse()
//        pauseAdContent.template = AdConstants.ADTYPE_PAUSE_VIDEO
//        pauseAdContent.advertises = pauseAdList
//        pauseAdContent.ad_id = AdConstants.AD_ID_VIDEO_DETAIL_PAUSE
//        adList.add(pauseAdContent)
//
//        // native - 插播
//        val instreamAdList = ArrayList<AdModel.AdvertisingResponse.AdvertisesResponse>()
//        val adInfoInstream = AdModel.AdvertisingResponse.AdvertisesResponse()
//        adInfoInstream.advertisers = AdConstants.ADVERTISER_IRON_SOURCE
//        adInfoInstream.advertising_id = "5024158"
//        adInfoInstream.cache_next = true
//        adInfoInstream.req_timeout = 10
//        adInfoInstream.auto_close = 6
//        instreamAdList.add(adInfoInstream)
//        val instreamAdContent = AdModel.AdvertisingResponse()
//        instreamAdContent.template = AdConstants.ADTYPE_INSTREAM_VIDEO
//        instreamAdContent.advertises = instreamAdList
//        instreamAdContent.ad_id = AdConstants.AD_ID_VIDEO_INSTREAM_AD
//        adList.add(instreamAdContent)
////
//        // 详情页插屏
//        val interstitialAdList = ArrayList<AdModel.AdvertisingResponse.AdvertisesResponse>()
//        val adInfoEntity2 = AdModel.AdvertisingResponse.AdvertisesResponse()
//        adInfoEntity2.advertisers = AdConstants.ADVERTISER_IRON_SOURCE
//        adInfoEntity2.advertising_id = "5024156"
//        adInfoEntity2.cache_next = false
//        adInfoEntity2.req_timeout = 10
//        adInfoEntity2.auto_close = 3
//        interstitialAdList.add(adInfoEntity2)
//
//        val interstitialAdContent = AdModel.AdvertisingResponse()
//        interstitialAdContent.template = AdConstants.ADTYPE_INTERSTITIAL
//        interstitialAdContent.advertises = interstitialAdList
//        interstitialAdContent.ad_id = AdConstants.AD_ID_VIDEO_DETAIL_INTERSTITIAL
//        adList.add(interstitialAdContent)
////
////
////            // 解锁激励
//        val rewardedAdList = ArrayList<AdModel.AdvertisingResponse.AdvertisesResponse>()
//        val adInfoEntity3 = AdModel.AdvertisingResponse.AdvertisesResponse()
//        adInfoEntity3.advertisers = AdConstants.ADVERTISER_IRON_SOURCE
//        adInfoEntity3.advertising_id = "5024152"
//        adInfoEntity3.cache_next = false
//        adInfoEntity3.req_timeout = 10
//        adInfoEntity3.auto_close = 6
//        rewardedAdList.add(adInfoEntity3)
//        val unlockRewardedAd = AdModel.AdvertisingResponse()
//        unlockRewardedAd.template = AdConstants.ADTYPE_REWARDED
//        unlockRewardedAd.advertises = rewardedAdList
//        unlockRewardedAd.ad_id = AdConstants.AD_ID_LOCK_REWARD_AD
//        adList.add(unlockRewardedAd)
//
//        // 解锁插屏
//        val lockInterstitialAdList = ArrayList<AdModel.AdvertisingResponse.AdvertisesResponse>()
//        val adInfoEntity5 = AdModel.AdvertisingResponse.AdvertisesResponse()
//        adInfoEntity5.advertisers = AdConstants.ADVERTISER_IRON_SOURCE
//        adInfoEntity5.advertising_id = "9514"
//        adInfoEntity5.cache_next = false
//        adInfoEntity5.req_timeout = 20
//        adInfoEntity5.auto_close = 6
//        lockInterstitialAdList.add(adInfoEntity5)
//        val lockInterstitialAdContent = AdModel.AdvertisingResponse()
//        lockInterstitialAdContent.template = AdConstants.ADTYPE_INTERSTITIAL
//        lockInterstitialAdContent.advertises = lockInterstitialAdList
//        lockInterstitialAdContent.ad_id = AdConstants.AD_ID_LOCK_INTERSTITIAL_AD
//        adList.add(lockInterstitialAdContent)
//
//        // 下载激励
//        val downloadRewardedAdList = ArrayList<AdModel.AdvertisingResponse.AdvertisesResponse>()
//        val adInfoEntity4 = AdModel.AdvertisingResponse.AdvertisesResponse()
//        adInfoEntity4.advertisers = AdConstants.ADVERTISER_IRON_SOURCE
//        adInfoEntity4.advertising_id = "5024152"
//        adInfoEntity4.cache_next = false
//        adInfoEntity4.req_timeout = 10
//        adInfoEntity4.auto_close = 6
//        downloadRewardedAdList.add(adInfoEntity4)
//        val downloadRewardedAd = AdModel.AdvertisingResponse()
//        downloadRewardedAd.template = AdConstants.ADTYPE_REWARDED
//        downloadRewardedAd.advertises = downloadRewardedAdList
//        downloadRewardedAd.ad_id = AdConstants.AD_ID_DOWNLOAD_REWARD_AD
//        adList.add(downloadRewardedAd)
//
//        // 下载插屏
//        val downloadInterstitialAdList = ArrayList<AdModel.AdvertisingResponse.AdvertisesResponse>()
//        val adInfoEntity6 = AdModel.AdvertisingResponse.AdvertisesResponse()
//        adInfoEntity6.advertisers = AdConstants.ADVERTISER_IRON_SOURCE
//        adInfoEntity6.advertising_id = "9514"
//        adInfoEntity6.cache_next = false
//        adInfoEntity6.req_timeout = 20
//        adInfoEntity6.auto_close = 6
//        downloadInterstitialAdList.add(adInfoEntity6)
//        val downloadInterstitialAdContent = AdModel.AdvertisingResponse()
//        downloadInterstitialAdContent.template = AdConstants.ADTYPE_INTERSTITIAL
//        downloadInterstitialAdContent.advertises = downloadInterstitialAdList
//        downloadInterstitialAdContent.ad_id = AdConstants.AD_ID_DOWNLOAD_INTERSTITIAL
//        adList.add(downloadInterstitialAdContent)
////
//        // 下载播放插屏
//        val downloadPlayAdList = ArrayList<AdModel.AdvertisingResponse.AdvertisesResponse>()
//        val adInfoEntity7 = AdModel.AdvertisingResponse.AdvertisesResponse()
//        adInfoEntity7.advertisers = AdConstants.ADVERTISER_IRON_SOURCE
//        adInfoEntity7.advertising_id = "5024156"
//        adInfoEntity7.cache_next = false
//        adInfoEntity7.req_timeout = 6
//        adInfoEntity7.auto_close = 6
//        downloadPlayAdList.add(adInfoEntity7)
//        val downloadPlayRewardedAd = AdModel.AdvertisingResponse()
//        downloadPlayRewardedAd.template = AdConstants.ADTYPE_REWARDED
//        downloadPlayRewardedAd.advertises = downloadPlayAdList
//        downloadPlayRewardedAd.ad_id = AdConstants.AD_ID_DOWNLOAD_PLAYER_INTERSTITIAL_AD
//        adList.add(downloadPlayRewardedAd)
////
////
//        // ReOpen Ad
//        val reopenInterstitialAdList = ArrayList<AdModel.AdvertisingResponse.AdvertisesResponse>()
//        val adInfoEntity8 = AdModel.AdvertisingResponse.AdvertisesResponse()
//        adInfoEntity8.advertisers = AdConstants.ADVERTISER_IRON_SOURCE
//        adInfoEntity8.advertising_id = "6368349"
//        adInfoEntity8.cache_next = false
//        adInfoEntity8.req_timeout = 20
//        adInfoEntity8.auto_close = 6
//        reopenInterstitialAdList.add(adInfoEntity8)
//        val reopenInterstitialAdContent = AdModel.AdvertisingResponse()
//        reopenInterstitialAdContent.template = AdConstants.ADTYPE_INTERSTITIAL
//        reopenInterstitialAdContent.advertises = reopenInterstitialAdList
//        reopenInterstitialAdContent.ad_id = AdConstants.AD_ID_REOPEN_AD
//        adList.add(reopenInterstitialAdContent)


//        // banner
//        val bannerAdList = ArrayList<AdModel.AdvertisingResponse.AdvertisesResponse>()
//        val adInfoEntityBanner = AdModel.AdvertisingResponse.AdvertisesResponse()
//        adInfoEntityBanner.advertisers = AdConstants.ADVERTISER_IRON_SOURCE
//        adInfoEntityBanner.advertising_id = "5024158"
//        adInfoEntityBanner.cache_next = true
//        adInfoEntityBanner.req_timeout = 20
//        adInfoEntityBanner.auto_close = 6
//        bannerAdList.add(adInfoEntityBanner)
//        // 详情页插屏
//        val bannerAd = AdModel.AdvertisingResponse()
//        bannerAd.template = AdConstants.ADTYPE_BANNER
//        bannerAd.advertises = bannerAdList
//        bannerAd.ad_id = AdConstants.AD_ID_VIDEO_DETAIL_BANNER
//        adList.add(bannerAd)

        //  Splash
//        val splashAdList = ArrayList<AdModel.AdvertisingResponse.AdvertisesResponse>()
//        val splash = AdModel.AdvertisingResponse.AdvertisesResponse()
//        splash.advertisers = AdConstants.ADVERTISER_ADMOB
//        splash.advertising_id = "9400"
//        splash.cache_next = false
//        splash.req_timeout = 6
//        splash.auto_close = 6
//        splashAdList.add(splash)
//        // 详情页插屏
//        val splashAdContent = AdModel.AdvertisingResponse()
//        splashAdContent.template = AdConstants.ADTYPE_SPLASH
//        splashAdContent.advertises = splashAdList
//        splashAdContent.ad_id = AdConstants.AD_ID_SPLASH
//        adList.add(splashAdContent)

        return  adList
    }
}



